package mo.offer_special;

import mo.common.TreeNode;

import java.util.*;

public class L044 {

    public List<Integer> largestValues(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        List<Integer> largest = new ArrayList<>();
        while (!queue.isEmpty()) {
            int len = queue.size();
            int max = Integer.MIN_VALUE;
            while (len > 0) {
                TreeNode node = queue.poll();
                len--;
                max = Math.max(max, node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            largest.add(max);
        }
        return largest;
    }


}
